//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __CAR_CARERR_H__
#define __CAR_CARERR_H__

//==========================================================================
//
//  ECODEs' layout:
//
//   3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
//   1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
//  +-+-+-+-+-+---------------------+-------------------------------+
//  |S|R|C|N|r|    Facility         |               Code            |
//  +-+-+-+-+-+---------------------+-------------------------------+
//
//      S - Severity - indicates success/fail
//
//          0 - Success
//          1 - Fail (COERROR)
//
//      R - reserved portion of the facility code.
//
//      C - reserved portion of the facility code.
//
//      N - reserved portion of the facility code.
//
//      r - reserved portion of the facility code. Reserved for internal
//              use. Used to indicate ECode values that are not status
//              values, but are instead message ids for display strings.
//
//      Facility - the facility code
//
//      Code - the facility's status code
//
typedef  long   ECode;
#define SUCCEEDED(x)    ((ECode)(x) >= 0)
#define FAILED(x)       ((ECode)(x)<0)

//
// Success codes
//
#ifndef NOERROR
#define NOERROR            ((ECode)0x00000000L)
#endif

#define S_FALSE         ((ECode)0x00000001L)
//#define FALSE    0
//
// Severity values
//
#define SEVERITY_SUCCESS    0
#define SEVERITY_ERROR      1

//
// Create an ECode value from component pieces
//
#define MAKE_ECODE(sev, fac, code) \
    ((ECode)(((unsigned long)(fac) << 16) | ((unsigned long)(sev) << 31) \
    | ((unsigned long)(code))))

//
// Map a WIN32 error value into a ECode
// Note: This assumes that WIN32 errors fall in the range -32k to 32k.
//
#define ECODE_FROM_WIN32(x) \
    (x ? ((ECode)(((x)&0x0000FFFF) | (FACILITY_WIN32<<16) | 0x80000000)) : 0)

//
// Define the facility codes
//
#define FACILITY_NULL               0x00
#define FACILITY_RPC                0x01
#define FACILITY_DISPATCH           0x02
#define FACILITY_STORAGE            0x03
#define FACILITY_ITF                0x04
#define FACILITY_WIN32              0x07 // WIN32 error codes
#define FACILITY_WINDOWS            0x08
#define FACILITY_SSPI               0x09
#define FACILITY_SECURITY           0x09
#define FACILITY_CONTROL            0x0A
#define FACILITY_CERT               0x0B
#define FACILITY_INTERNET           0x0C
#define FACILITY_MEDIASERVER        0x0D
#define FACILITY_MSMQ               0x0E
#define FACILITY_SETUPAPI           0x0F
#define FACILITY_SCARD              0x10
#define FACILITY_COMPLUS            0x11
#define FACILITY_AAF                0x12
#define FACILITY_URT                0x13 // .Net Univeral Runtime's error codes
#define FACILITY_ACS                0x14
#define FACILITY_DPLAY              0x15
#define FACILITY_UMI                0x16
#define FACILITY_SXS                0x17
#define FACILITY_WINDOWS_CE         0x18
#define FACILITY_HTTP               0x19
#define FACILITY_BACKGROUNDCOPY     0x20
#define FACILITY_CONFIGURATION      0x21
#define FACILITY_ELASTOS            0x40 // Elastos extended error codes
#define FACILITY_CAR                0x41 // CAR extended error codes
#define FACILITY_MOBILE             0x42 // Mobile Applications extended error

#define RPC_ERROR(code)                 MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_RPC, code)
#define DISPATCH_ERROR(code)            MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_DISPATCH, code)
#define STORAGE_ERROR(code)             MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_STORAGE, code)
#define ITF_ERROR(code)                 MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_ITF, code)
#define WIN32_ERROR(code)               MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_WIN32, code)
#define WINDOWS_ERROR(code)             MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_WINDOWS, code)
#define SSPI_ERROR(code)                MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_SSPI, code)
#define SECURITY_ERROR(code)            MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_SECURITY, code)
#define CONTROL_ERROR(code)             MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_CONTROL, code)
#define CERT_ERROR(code)                MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_CERT, code)
#define INTERNET_ERROR(code)            MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_INTERNET, code)
#define MEDIASERVER_ERROR(code)         MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_MEDIASERVER, code)
#define MSMQ_ERROR(code)                MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_MSMQ, code)
#define SETUPAPI_ERROR(code)            MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_SETUPAPI, code)
#define SCARD_ERROR(code)               MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_SCARD, code)
#define COMPLUS_ERROR(code)             MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_COMPLUS, code)
#define AAF_ERROR(code)                 MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_AAF, code)
#define URT_ERROR(code)                 MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_URT, code)
#define ACS_ERROR(code)                 MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_ACS, code)
#define DPLAY_ERROR(code)               MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_DPLAY, code)
#define UMI_ERROR(code)                 MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_UMI, code)
#define SXS_ERROR(code)                 MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_SXS, code)
#define WINDOWS_CE_ERROR(code)          MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_WINDOWS_CE, code)
#define HTTP_ERROR(code)                MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_HTTP, code)
#define BACKGROUNDCOPY_ERROR(code)      MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_BACKGROUNDCOPY, code)
#define CONFIGURATION_ERROR(code)       MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_CONFIGURATION, code)
#define ELASTOS_ERROR(code)             MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_ELASTOS, code)
#define CAR_ERROR(code)                 MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_CAR, code)
#define MOBILE_ERROR(code)              MAKE_ECODE(SEVERITY_ERROR, \
            FACILITY_MOBILE, code)

#define RPC_SUCCESS(code)               MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_RPC, code)
#define DISPATCH_SUCCESS(code)          MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_DISPATCH, code)
#define STORAGE_SUCCESS(code)           MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_STORAGE, code)
#define ITF_SUCCESS(code)               MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_ITF, code)
#define WIN32_SUCCESS(code)             MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_WIN32, code)
#define WINDOWS_SUCCESS(code)           MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_WINDOWS, code)
#define SSPI_SUCCESS(code)              MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_SSPI, code)
#define SECURITY_SUCCESS(code)          MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_SECURITY, code)
#define CONTROL_SUCCESS(code)           MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_CONTROL, code)
#define CERT_SUCCESS(code)              MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_CERT, code)
#define INTERNET_SUCCESS(code)          MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_INTERNET, code)
#define MEDIASERVER_SUCCESS(code)       MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_MEDIASERVER, code)
#define MSMQ_SUCCESS(code)              MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_MSMQ, code)
#define SETUPAPI_SUCCESS(code)          MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_SETUPAPI, code)
#define SCARD_SUCCESS(code)             MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_SCARD, code)
#define COMPLUS_SUCCESS(code)           MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_COMPLUS, code)
#define AAF_SUCCESS(code)               MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_AAF, code)
#define URT_SUCCESS(code)               MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_URT, code)
#define ACS_SUCCESS(code)               MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_ACS, code)
#define DPLAY_SUCCESS(code)             MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_DPLAY, code)
#define UMI_SUCCESS(code)               MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_UMI, code)
#define SXS_SUCCESS(code)               MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_SXS, code)
#define WINDOWS_CE_SUCCESS(code)        MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_WINDOWS_CE, code)
#define HTTP_SUCCESS(code)              MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_HTTP, code)
#define BACKGROUNDCOPY_SUCCESS(code)    MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_BACKGROUNDCOPY, code)
#define CONFIGURATION_SUCCESS(code)     MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_CONFIGURATION, code)
#define ELASTOS_SUCCESS(code)           MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_ELASTOS, code)
#define CAR_SUCCESS(code)               MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_CAR, code)
#define MOBILE_SUCCESS(code)            MAKE_ECODE(SEVERITY_SUCCESS, \
            FACILITY_MOBILE, code)

//==========================================================================
// Error definitions follow
//==========================================================================

// Catastrophic failure
#define E_UNEXPECTED                ((ECode)0x8000FFFFL)

//==========================================================================
// Macros and constants for FACILITY_ITF error codes
//==========================================================================

// Not implemented
#define E_NOT_IMPLEMENTED                   ((ECode)0x80004001L)

// No such interface supported
#define E_NO_INTERFACE               ((ECode)0x80004002L)

// Invalid pointer
// TODO: Use E_INVALIDARG replaceing E_INVALID_POINTER
#define E_INVALID_POINTER                   ((ECode)0x80004003L)

// Operation aborted
#define E_ABORT                     ((ECode)0x80004004L)

// Unspecified error
#define E_FAIL                      ((ECode)0x80004005L)

// No default constructor in class
#define E_NO_DEFAULT_CTOR            ((ECode)0x80004006L)

// Class does not support aggregation (or class object is remote)
#define E_CLASS_NO_AGGREGATION       ((ECode)0x80040110L)

// ClassFactory cannot supply requested class
#define E_CLASS_NOT_AVAILABLE   ((ECode)0x80040111L)

// CATID does not exist
#define E_CATID_NOT_EXIST          ((ECode)0x80040160L)

// Description not found
#define CAT_E_NODESCRIPTION         ((ECode)0x80040161L)

//Aggregate failed
#define E_AGGREGATE_FAILED      ((ECode)0x80040162L)
//Unaggregate failed
#define E_UNAGGREGATE_FAILED      ((ECode)0x80040163L)

//
// IConnectionPoint status codes
//
// there is no connection for this connection id
#define CONNECT_E_NOCONNECTION      ((ECode)0x80040200L)

// this implementation's limit for advisory connections has been reached
#define CONNECT_E_ADVISELIMIT       ((ECode)0x80040201L)

// connection attempt failed
#define CONNECT_E_CANNOTCONNECT     ((ECode)0x80040202L)

// must use a derived interface to connect
#define CONNECT_E_OVERRIDDEN        ((ECode)0x80040203L)

//==========================================================================
// Macros and constants for FACILITY_WIN32 error codes
//==========================================================================
// General access denied error
#define E_ACCESS_DENIED  ((ECode)0x80070005L)

// Ran out of memory
#define E_OUT_OF_MEMORY   ((ECode)0x8007000EL)

// One or more arguments are invalid
#define E_INVALID_ARGUMENT    ((ECode)0x80070057L)

//==========================================================================
// Macros and constants for FACILITY_WINDOWS error codes
//==========================================================================
// Not all the requested interfaces were available
#define CO_NOTALLINTERFACES ((ECode)0x00080012L)

#endif // __CAR_CARERR_H__
